#include "ThreadPool.h"
#include <iostream>
#include <chrono>
#include <thread>

using u64_t = unsigned long long;

class MyTask : public Task
{
public:
	MyTask(int begin, int end) :begin(begin), end(end) {}
	Any run()
	{
		printf("tid: %d begin!\n", std::this_thread::get_id());
		std::this_thread::sleep_for(std::chrono::seconds(3));
		u64_t sum = 0;
		for (u64_t i = begin; i <= end; i++)
		{
			sum += i;
		}
		printf("tid: %d end!\n", std::this_thread::get_id());
		return sum;
	}
private:
	int begin;
	int end;
};

int main()
{
	{
		ThreadPool pool;
		pool.setMode(PoolMode::MODE_CACHED);
		pool.start(2);
		Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		/*u64_t sum1 = res1.get().cast<u64_t>();
		std::cout << sum1 << std::endl;*/
	}
	std::cout << "main is over!\n";
	//getchar();
#if 0
	{
		ThreadPool pool;
		pool.setMode(PoolMode::MODE_CACHED);
		pool.start(4);

		Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		Result res2 = pool.submitTask(std::make_shared<MyTask>(100000001, 200000000));
		Result res3 = pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));


		u64_t sum1 = res1.get().cast<u64_t>();
		u64_t sum2 = res2.get().cast<u64_t>();
		u64_t sum3 = res3.get().cast<u64_t>();

		std::cout << sum1 + sum2 + sum3 << std::endl;
	}

	getchar();
#endif
	return 0;
}